package com.div.tools.screen

import android.app.Activity
import android.content.res.Resources
import android.view.View

class ScreenTool {

    companion object {
        private var flag = 0

        fun getStatusBarHeight(): Int {
            val resources = Resources.getSystem()
            return resources.getDimensionPixelSize(resources.getIdentifier("status_bar_height", "dimen", "android"))
        }

        fun getNavigationBarHeight(): Int {
            val resources = Resources.getSystem()
            return resources.getDimensionPixelSize(resources.getIdentifier("navigation_bar_height", "dimen", "android"))
        }

        fun hideStatusBar(activity: Activity) {
            val flag = (View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                            or View.SYSTEM_UI_FLAG_FULLSCREEN
                            or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    ) or this.flag
            activity.window.decorView.systemUiVisibility = flag
            this.flag = flag
        }

        fun hideNavBar(activity: Activity) {
            val flag = (View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                            or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) or this.flag
            activity.window.decorView.systemUiVisibility = flag
            this.flag = flag
        }

        fun showFullScreen(activity: Activity) {
            activity.window.decorView.systemUiVisibility = (
                    View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    or View.SYSTEM_UI_FLAG_FULLSCREEN
                    or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    )
        }

        fun setStatusBarColor(activity: Activity, resId: Int) {
            activity.window.statusBarColor = activity.resources.getColor(resId)
        }

        fun setNavBarColor(activity: Activity, resId: Int) {
            activity.window.navigationBarColor = activity.resources.getColor(resId)
        }

        /**
         * 状态栏透明，显示布局颜色
         * @param isLight 设置状态栏的深浅状态
         */
        fun setStatusBarTransparent(activity: Activity, isLight: Boolean){
            var flag = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            if (isLight) {
                flag = flag or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            }
            flag = flag or this.flag
            activity.window.decorView.systemUiVisibility = flag
            activity.window.statusBarColor = activity.resources.getColor(android.R.color.transparent)
            this.flag = flag
        }

        fun setNavBarTransparent(activity: Activity, isLight: Boolean){
            var flag = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            if (isLight) {
                flag = flag or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
            }
            flag = flag or this.flag
            activity.window.decorView.systemUiVisibility = flag
            activity.window.navigationBarColor = activity.resources.getColor(android.R.color.transparent)
            this.flag = flag
        }
    }
}